<template>
  <div class='container'>
    <el-card class="box-card">
      <!-- 搜索框 -->
      <div class="sub-main">
        <el-form ref="form" :model="formlist" label-width="80px">
        <el-form-item label="学科名称">
          <el-input v-model="formlist.subjectName" style="width: 200px;margin-right:10px ;"></el-input>
          <el-button size="small" @click="formlist.subjectName=''">清除</el-button>
          <el-button  size="small" type="primary" @click="OnSearch">搜索</el-button>
          <el-button size="small" icon="el-icon-edit" type="success" @click="addSubject">新增学科</el-button>

        </el-form-item>
      </el-form>
      </div>
      <!-- 提示框 -->
      <el-alert
        :title="`数据一共${this.pageTotal}条`"
        type="info">
      </el-alert>
      <!-- 表格 -->
        <div class="sub-table">
          <el-table
          v-loading="loading"
          :data="SubjectsListData"
          style="width: 100%">
          <el-table-column
            type="index"
            label="序号"
            >
          </el-table-column>
          <el-table-column
            prop="subjectName"
            label="学科名称"
          >
          </el-table-column>
          <el-table-column
            prop="username"
            label="创建者"
          >
          </el-table-column>
          <el-table-column
            label="创建日期"
            width="240"
          >
            <template slot-scope="{row}">
              {{row.addDate | date}}
            </template>
          </el-table-column>
          <el-table-column
            prop="isFrontDisplay"
            label="前台是否显示"
          >
            <template slot-scope="{row}">
              <div v-if="row.isFrontDisplay===1">是</div>
              <div v-else>否</div>
            </template>
          </el-table-column>
          <el-table-column
            prop="twoLevelDirectory"
            label="二级目录"
          >
          </el-table-column>
          <el-table-column
            prop="tags"
            label="标签"
          >
          </el-table-column>
          <el-table-column
            prop="totals"
            label="题目数量"
          >
          </el-table-column>
          <el-table-column
            label="操作"
            width="260"
          >
            <template slot-scope="{row}">
              <el-button type="text" size="small" @click="$router.push(`directorys?id=${row.id}&name=${row.subjectName}`)">学科分类</el-button>
              <el-button type="text" size="small" @click="$router.push(`tags?id=${row.id}&name=${row.subjectName}`)">学科标签</el-button>
              <el-button type="text" size="small" @click="onEdit(row)">修改</el-button>
              <el-button type="text" size="small" @click="onDel(row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        </div>

      <!-- 分页 -->
     <page-nation :formlist="formlist" :total="pageTotal" @on-success="GetSubjectsList()"/>
    </el-card>

    <!-- 新增弹窗 -->
     <el-dialog
      :title="this.addForm.id?'修改学科':'新增学科'"
      :visible.sync="addDialgVisible"
      width="30%"
      @close="addDialogClose"
      >
      <el-form :rules="addRules" ref="addForm" :model="addForm" label-width="80px">
        <el-form-item label="学科名称" prop="subjectName">
          <el-input v-model="addForm.subjectName"></el-input>
        </el-form-item>
        <el-form-item label="是否显示">
          <el-switch @change="onChange($event)" inactive-value="0" active-value="1" v-model="delivery"></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialgVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOnSave">确 定</el-button>
      </span>
    </el-dialog>
  </div>

</template>

<script>
import { list, add, update, remove } from '@/api/hmmm/subjects.js'
import PageNation from '../components/PageNation'
import dayjs from 'dayjs'
export default {
  name: 'subjects',
  components: {
    PageNation
  },
  data () {
    return {
      loading: true, // 加载数据
      SubjectsListData: [], // 学科列表
      pageTotal: 0, // 共几页
      formlist: {
        page: 1,
        pagesize: 10 // 页尺寸
      },
      addDialgVisible: false,
      // 新增学科
      addForm: {
        subjectName: '',
        isFrontDisplay: '0'
      },
      addRules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' },
          { min: 1, message: '最小长度为1', trigger: 'blur' }
        ]
      },
      // 新增学科是否显示
      delivery: ''
    }
  },

  created () {
    this.GetSubjectsList()
  },

  methods: {
    // 获取学科列表
    async GetSubjectsList () {
      const res = await list(this.formlist)
      // console.log(res)
      this.loading = false
      this.SubjectsListData = res.data.items
      this.pageTotal = res.data.counts
      // console.log(this.SubjectsListData)
    },
    // 新增学科
    addSubject () {
      this.addDialgVisible = true
    },
    // 点击修改
    onEdit (row) {
      // console.log(row)
      if (row.isFrontDisplay === 1) {
        this.delivery = '1'
      }
      if (row.isFrontDisplay === 0) {
        this.delivery = '0'
      }
      // console.log(this.delivery)
      this.addDialgVisible = true
      // 根据row.id 发送请求获取最新数据
      this.addForm = { ...row }
      // console.log(this.addForm)
    },
    // 新增学科点击确定
    async addOnSave () {
      await this.$refs.addForm.validate()
      if (this.addForm.id) {
        await update(this.addForm)
        this.$message.success('修改成功')
      } else {
        await add(this.addForm)
        this.$message.success('添加成功')
      }
      this.addDialgVisible = false
      this.GetSubjectsList()
    },
    // 开关变化
    onChange (val) {
      // console.log(val)
      this.addForm.isFrontDisplay = val
    },
    // ×掉弹窗
    addDialogClose () {
      // 重置表单/ 重置校验
      this.$refs.addForm.resetFields()
      this.addForm = {
        subjectName: '',
        isFrontDisplay: 0
      }
    },
    // 删除部门
    onDel (row) {
      // console.log(row)
      this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // console.log(this.obj)
        await remove(row)
        this.$message.success('删除成功')
        this.GetSubjectsList()
      }).catch(() => {
      })
    },
    // 搜索学科
    OnSearch () {
      this.GetSubjectsList()
    }
  },
  // 时间过滤器
  filters: {
    date (val) {
      return dayjs(dayjs(val).$d).format('YYYY-MM-DD HH:mm:ss')
    }
  }
}
</script>

<style scoped lang='scss'>
// .sub-main{
// display:flex;
// }
.box-card{
  margin: 10px;
}
.sub-table{
  margin-bottom: 20px;
}
</style>
